﻿'© 2004 - 2009 ActiveModules, Inc. All Rights Reserved
Imports Microsoft.ApplicationBlocks.Data
Namespace DotNetNuke.Modules.ActiveForums.Data
    Public Class AttachController
        Inherits Connection
        Public Function Attach_Save(ByVal ai As AttachInfo) As Integer
            Return CType(SqlHelper.ExecuteScalar(connectionString, dbPrefix & "Attachments_Save", ai.AttachID, ai.ContentId, ai.UserID, ai.Filename, ai.FileData, ai.ContentType, ai.FileSize, ai.AllowDownload, ai.DisplayInline, ai.ParentAttachId), Integer)
        End Function

        Public Function Attach_List(ByVal TopicId As Integer, ByVal ReplyId As Integer, ByVal ContentId As Integer, ByVal UserId As Integer) As List(Of AttachInfo)
            Dim al As New List(Of AttachInfo)
            Using dr As IDataReader = SqlHelper.ExecuteReader(connectionString, dbPrefix & "Attachments_List", TopicId, ReplyId, ContentId, UserId)
                While dr.Read
                    al.Add(FillInfo(dr))
                End While
                dr.Close()
            End Using
            Return al
        End Function
        Public Function Attach_ListMyFiles(ByVal UserId As Integer, ByVal RowIndex As Integer, ByVal MaxRows As Integer, ByVal SortColumn As String, ByVal Sort As String) As List(Of AttachInfo)
            Dim al As New List(Of AttachInfo)
            Using dr As IDataReader = SqlHelper.ExecuteReader(connectionString, dbPrefix & "Attachments_ListMyFiles", UserId, RowIndex, MaxRows, SortColumn, Sort)
                While dr.Read
                    al.Add(FillInfo(dr))
                End While
                dr.Close()
            End Using
            Return al
        End Function
        Public Function Attach_ListAttachFiles(ByVal UserId As Integer, ByVal AttachIds As String) As List(Of AttachInfo)
            Dim al As New List(Of AttachInfo)
            Using dr As IDataReader = SqlHelper.ExecuteReader(connectionString, dbPrefix & "Attachments_ListAttachFiles", UserId, AttachIds)
                While dr.Read
                    al.Add(FillInfo(dr))
                End While
                dr.Close()
            End Using
            Return al
        End Function
        Public Function Attach_Get(ByVal AttachID As Integer, ByVal ContentId As Integer, ByVal UserId As Integer, ByVal WithSecurity As Boolean) As AttachInfo
            Dim ai As AttachInfo = Nothing
            Using dr As IDataReader = SqlHelper.ExecuteReader(connectionString, dbPrefix & "Attachments_Get", AttachID, ContentId, UserId, WithSecurity)
                While dr.Read()
                    ai = FillInfo(dr)
                End While
                dr.Close()
            End Using
            Return ai
        End Function
        Public Function GetAttachIds(ByVal AuthorId As Integer, ByVal ContentId As Integer) As String
            Dim attachids As String = String.Empty
            Using dr As IDataReader = SqlHelper.ExecuteReader(connectionString, dbPrefix & "Attachments_ListForPost", AuthorId, ContentId)
                While dr.Read()
                    attachids &= dr("AttachId").ToString & ";"
                End While
                dr.Close()
            End Using
            Return attachids
        End Function
        Public Sub SaveToContent(ByVal ContentId As Integer, ByVal AttachId As Integer, FileURL As String, FileName As String, DisplayLink As Boolean, ContentType As String)
            SqlHelper.ExecuteNonQuery(connectionString, dbPrefix & "Attachments_SaveToContent", ContentId, AttachId, FileURL, FileName, DisplayLink, ContentType)
        End Sub
        Public Sub Attach_Delete(ByVal AttachId As Integer, ByVal ContentId As Integer)
            SqlHelper.ExecuteNonQuery(connectionString, dbPrefix & "Attachments_Content_Delete", AttachId, ContentId)
        End Sub
        Public Sub Attach_Delete(ByVal AttachId As Integer, ByVal ContentId As Integer, ByVal UserId As Integer)
            SqlHelper.ExecuteNonQuery(connectionString, dbPrefix & "Attachments_Delete", AttachId, ContentId, UserId)
        End Sub
        Private Function FillInfo(ByVal dr As IDataRecord) As AttachInfo
            Dim ai As New AttachInfo
            With ai
                .AttachID = CInt(dr("AttachId"))
                Try
                    If dr.Item("ContentId") Is Nothing Then
                        .ContentId = -1
                    Else
                        .ContentId = CInt(dr("ContentId"))
                    End If
                Catch ex As Exception
                    .ContentId = -1
                End Try

                Try
                    .PostID = CInt(dr("PostId"))
                Catch ex As Exception
                    .PostID = -1
                End Try

                .UserID = CInt(dr("UserId"))
                .Filename = CStr(dr("FileName"))
                .AllowDownload = CBool(dr("AllowDownload"))
                .DisplayInline = CBool(dr("DisplayInline"))
                .ParentAttachId = CInt(dr("ParentAttachId"))
                Try
                    .FileUrl = dr("FileURL").ToString()
                Catch ex As Exception
                    .FileUrl = Nothing
                End Try
                Try
                    .CanRead = dr("CanRead").ToString
                Catch ex As Exception
                    .CanRead = String.Empty
                End Try
                If Not dr("FileData") Is DBNull.Value Then
                    .FileData = CType(dr("FileData"), Array)
                Else
                    .FileData = Nothing
                End If

                .ContentType = CStr(dr("ContentType"))
                .FileSize = CInt(dr("FileSize"))
            End With
            Return ai
        End Function

    End Class

End Namespace

